<?php

namespace App\Http\Controllers\Admin\Permission;

use App\Http\Controllers\Admin\BaseController;
use App\Repository\AdminRepository;
use App\Repository\RoleRepository;
use Illuminate\Http\Request;
use AdminService;


class AdminController extends BaseController
{

    protected $admin;

    protected $role;

    public function __construct(AdminRepository $admin, RoleRepository $role)
    {
        $this->middleware('permission:admin_manage');
        $this->admin = $admin;
        $this->role = $role;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $admins = $this->admin->allWithUser();

        $roles = $this->role->all();

        return view('admin.permission.admin', ['admins' => $admins, 'roles' => $roles]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'number' => 'required',
            'password' => 'required|min:6'
        ], [
            'name.required' => '必须填写学号',
            'password.required' => '必须填写密码',
            'password.min' => '密码最少6位'
        ]);

        $admin = $request->all();

        AdminService::add($admin['number'], $admin['password']);

        return $this->success('增加管理员成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if(!\Entrust::can('admin_delete')){
            abort(403, '你没有权限进行此操作');
        }

        $this->admin->delete($id);

        return $this->success('删除管理员成功', url('admin/permission/admin'));
    }
}
